﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Group16.Class_Library
{
    public class InvoiceManager
    {
        private int _quantity = 0;
        private List<Invoice> _invoices;
        public int Quantity
        {
            get { return _quantity; }
            set { _quantity = value; }
        }
        public List<Invoice> Invoices
        {
            get { return _invoices; }
            set { _invoices = value; }
        }
        /// <summary>
        /// Find out all invoice appropriated with given invoice code
        /// </summary>
        /// <param name="series"></param>
        public void SearchDevices(string InvoiceCode)
        {
            try
            {
                string comText = "Select * from Invoice Where InvoiceCode = '" + InvoiceCode + "'";
                using (SqlDataReader reader = (SqlDataReader)DataBaseInteraction.ExecuteCommand(comText, 2))
                {
                    Invoices = new List<Invoice>();
                    while (reader.Read())
                    {
                        Invoice iv = new Invoice();
                        iv.InvoiceCode = reader.GetString(0);
                        iv.DateMade = reader.GetDateTime(1);
                        iv.Total = (int)reader.GetValue(2);
                        Invoices.Add(iv);
                    }
                }

            }
            catch (SqlException se)
            {
                MessageBox.Show("Problem in interaction with database" + se.Message);
            }
            finally
            {
                DataBaseInteraction.Connection.Close();
            }
        }
        public void GetInvoice(DateTime strt, DateTime end)
        {
            try
            {

                string comText = "Select * from Invoice Where DateMade BETWEEN '" + strt.ToShortDateString() + "' AND '" + end.ToShortDateString() +"'";
                using (SqlDataReader reader = (SqlDataReader)DataBaseInteraction.ExecuteCommand(comText, 2))
                {
                    Invoices = new List<Invoice>();
                    while (reader.Read())
                    {
                        Invoice iv = new Invoice();
                        iv.InvoiceCode = reader.GetString(0);
                        iv.DateMade = reader.GetDateTime(1);
                        iv.Total = (int)reader.GetValue(2);
                        Invoices.Add(iv);
                    }
                }

            }
            catch (SqlException se)
            {
                MessageBox.Show("Problem in interaction with database" + se.Message);
            }
            finally
            {
                DataBaseInteraction.Connection.Close();
            }
        }
    }
}
